/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.testis.utils;

import org.testis.nicmodule.core.NicModuleExceptionType;
import org.testis.nicmodule.core.NicModuleException;
import java.io.*;

/**
 * writes contents to the file renames if the suggested name exists
 * @param filename to be written
 * @param contents to be written to the file
 * @author alperb
 */
public class WpaFileOp {
    public static boolean writeToFile(String filename, String contents){
       try {
            File name = new File(filename);
            if (name.exists()){
                File nameTmp = new File(filename + ".old");
                name.renameTo(nameTmp);
                name.delete();
            }                             
            FileWriter fstream = new FileWriter(filename,true);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(contents);
            out.close();
            return true;
            
        } catch (IOException ex) {
            throw new NicModuleException(NicModuleExceptionType.UNEXPLAINED,
                                         "WpaFileOp:writeToFile IOException " +
                                         ex.getMessage());
        } catch (Exception ex) {
            throw new NicModuleException(NicModuleExceptionType.UNEXPLAINED,
                                         "WpaFileOp:writeToFile exception " +
                                         ex.getMessage());
        }
    }
    

}
